# coding=UTF-8
'''
    百度股市通爬虫
'''
import scrapy
import re
from baidu.items import BaiduItem


class BaiduStockSpider(scrapy.Spider):
    '''
        爬虫类
    '''
    # 爬虫名
    name = 'baidustock'

    # 被爬取的网页
    start_urls = [
        'https://gupiao.baidu.com/stock/sz000001.html'
    ]

    def parse(self, response):
        '''
            对请求进行解析，将爬取的数据保存为字符串，对数据进行处理，得到合适的Item
        '''
        # 解析response，进行初步定位
        quote = response.xpath(
            '//div[@class="bets-content"]'
        )

        # 定位收盘价
        closing_price = quote.xpath(
            'div[@class="line2"]/dl[1]/dd/text()'
        ).extract_first()

        # 定位开盘价
        opening_price = quote.xpath(
            'div[@class="line1"]/dl[1]/dd/text()'
        ).extract_first()

        # 定位最高价
        maximun_price = quote.xpath(
            'div[@class="line1"]/dl[3]/dd/text()'
        ).extract_first()

        # 定位最低价
        minimun_price = quote.xpath(
            'div[@class="line2"]/dl[3]/dd/text()'
        ).extract_first()

        # 定位内盘成交量
        turnover_inner = quote.xpath(
            'div[@class="line1"]/dl[5]/dd/text()'
        ).re_first(r"\d*\.\d{2}")

        # 定位外盘成交量
        turnover_outer = quote.xpath(
            'div[@class="line2"]/dl[5]/dd/text()'
        ).re_first(r"\d*\.\d{2}")

        # 定位成交额
        turnover_volume = quote.xpath(
            'div[@class="line1"]/dl[6]/dd/text()'
        ).re_first(r"\d*\.\d{2}")

        # 定位换手率
        turnover_rate = quote.xpath(
            'div[@class="line2"]/dl[2]/dd/text()'
        ).re_first(r"\d*\.\d{2}")

        # 定位流通市值
        efamc = quote.xpath(
            'div[@class="line1"]/dl[8]/dd/text()'
        ).re_first(r"\d*\.\d{2}")

        # 定位总市值
        mktcap = quote.xpath(
            'div[@class="line2"]/dl[8]/dd/text()'
        ).re_first(r"\d*\.\d{2}")

        item = BaiduItem()

        # 收盘价 单位：元
        item['closing_price'] = float(closing_price)

        # 开盘价 单位：元
        item['opening_price'] = float(opening_price)

        # 最高价 单位：元
        item['maximun_price'] = float(maximun_price)

        # 最低价 单位：元
        item['minimun_price'] = float(minimun_price)

        # 成交量turnover由内盘turnover_inner、外盘turnover_outer两部分组成，单位：股
        item['turnover'] = int(float(turnover_inner) * 1000000) + \
            int(float(turnover_outer) * 1000000)

        # 成交额 单位：元
        item['turnover_volume'] = int(float(turnover_volume) * 10000000)

        # 换手率
        item['turnover_rate'] = float(turnover_rate)

        # 流通市值 单位：元
        item['efamc'] = int(float(efamc) * 100000000)

        # 总市值 单位：元
        item['mktcap'] = int(float(mktcap) * 100000000)

        yield item
